package main

import (
	"math/big"
	"fmt"
	"cryptogm/elliptic"
	"cryptogm/sm/sm2"
	"time"
	"crypto/sha256"
	"crypto/rand"
)

func calGmPointMul(){
	c:=elliptic.P256()
	x1,_:=new(big.Int).SetString("32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",16)
	y1,_:=new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0",16)
	//x2:=x1
	//y2:=y1
	elliptic.Hexprint(c.Params().P.Bytes())
	k1,_:=new(big.Int).SetString("BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0",16)

	x3,y3:=c.ScalarBaseMult(k1.Bytes())
	elliptic.Hexprint(x3.Bytes())
	elliptic.Hexprint(y3.Bytes())
	k2,_:=new(big.Int).SetString("3",16)
	x4,y4:=c.ScalarMult(x1,y1,k2.Bytes())
	fmt.Println("double point:")
	elliptic.Hexprint(x4.Bytes())
	elliptic.Hexprint(y4.Bytes())
}


func sm2_test() {
	msg := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	prism2, _ := sm2.GenerateKey(rand.Reader)
	fmt.Println("p:")
	elliptic.Hexprint(prism2.Params().P.Bytes())
	fmt.Println("skey:")
	elliptic.Hexprint(prism2.D.Bytes())
	fmt.Println("pkeyx:")
	elliptic.Hexprint(prism2.X.Bytes())
	fmt.Println("pkeyy:")
	elliptic.Hexprint(prism2.Y.Bytes())

	hfunc := sha256.New()
	hfunc.Write(msg)
	hash := hfunc.Sum(nil)

	var r, s *big.Int

	fmt.Println("sm2")
	fmt.Println("sm2 sign")
	t1:=time.Now();
	for i := 0; i < 10000; i++ {
	r, s, _ = sm2.Sign(rand.Reader, prism2, hash)
	}
	elspsed:=time.Since(t1);
	fmt.Println(elspsed)
	fmt.Println("sm2 verify")
	t2:=time.Now()
	for i:=0;i<10000;i++{
		sm2.Verify(&prism2.PublicKey, hash, r, s)
	}
	elspsed2:=time.Since(t2)
	fmt.Println(elspsed2)
}

func sm2_correct_test(){

}

func main() {
//	calGmPointMul()
//	elliptic.TestP256_Point()
//	elliptic.Test_amd64()
//	elliptic.Test_p256InternalFunc()
//	elliptic.Test_p256Func()
//	elliptic.TestP256_Point()
	sm2_test()
}
